package edu.usx.server.controller;

import edu.usx.server.pojo.User;
import edu.usx.server.pojo.UserLoginParam;
import edu.usx.server.pojo.RespBean;
import edu.usx.server.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Api("LoginController")
@RestController
public class LoginController {
    @Autowired
    private IUserService iUserService;
    @ApiOperation(value="登录之后返回token")
    @PostMapping("/user/login")
    public RespBean login(@RequestBody UserLoginParam adminLoginParam, HttpServletRequest request){
        return iUserService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(), adminLoginParam.getCode(), request);
    }

    @ApiOperation(value="退出登录")
    @PostMapping("/user/logout")
    public RespBean logout(){
        return RespBean.success("成功退出登录！");
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/user/info")
    public User getAdminInfo(Principal principal){
        if(principal==null){
            return null;
        }
        String username= principal.getName();
        //根据用户名获取完整用户对象
        User user = iUserService.getUserByUserName(username);
        user.setPassword(null);
        return user;
    }
}
